package com.tianpengtech.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.jfinal.kit.Prop;
import com.tianpengtech.front.mobile.common.util.ToolMD5;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;



/**
 * Created by ggd on 2017/8/1 0001.
 */
public class SendSmsMsg {
    private static Prop prop = new Prop("sms.properties");
    public static final boolean isSend=prop.getBoolean("isSend");

    //产品名称:云通信短信API产品,开发者无需替换
    private static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    private static final String domain = "dysmsapi.aliyuncs.com";
    //    static final String accessKeyId = "LTAIvQPdztlGanRB";
//    static final String accessKeySecret = "KSxwLrQR7BdijGWYzGv21T3HTwTvMO";
    
    
    public static final String TEMPLATE_REG_SMS="SMS_76595520";//发送注册验证码
    public static final String TEMPLATE_PASS_BACK_SMS="SMS_76595518";//忘记密码验证码
    
    
    private static final String accessKeyId = prop.get("accessKeyId");
    private static final String accessKeySecret = prop.get("accessKeySecret");
    public static boolean send(String mobile,String code,String templateCode) {
        IClientProfile profile = DefaultProfile.getProfile("cn-beijing", accessKeyId, accessKeySecret);
        try {
            DefaultProfile.addEndpoint("cn-beijing", "cn-beijing", product, domain);
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
        IAcsClient acsClient = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(mobile);
//        request.setSignName("铂数网络");
//        request.setTemplateCode("SMS_76595520");
        request.setSignName(prop.get("signName"));
        request.setTemplateCode(templateCode);
        request.setTemplateParam("{\"code\":\""+code+"\",\"product\":\"象漾\"}");
        SendSmsResponse sendSmsResponse = null;
        try {
            sendSmsResponse = acsClient.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
        if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
            return true;
        }
        return false;
    }



































//    private static Prop prop = new Prop("sms.properties");
//    public static final String URL=prop.get("url");
//    public static final String USERID=prop.get("userid");
//    public static final String APIKEY=prop.get("apikey");
//    public static final String TEMPLATEID=prop.get("templateid");
//    public static final boolean isSend=prop.getBoolean("isSend");
//
//    public static boolean send(String mobile,String code) {
//
//        try {
//            //创建连接
//            java.net.URL url = new URL(URL);
//            HttpURLConnection connection = (HttpURLConnection) url
//                    .openConnection();
//            connection.setDoOutput(true);
//            connection.setDoInput(true);
//            connection.setRequestMethod("POST");
//            connection.setUseCaches(false);
//            connection.setInstanceFollowRedirects(true);
//            connection.setRequestProperty("Content-Type",
//                    "application/x-www-form-urlencoded");
//
//            connection.connect();
//
//            //POST请求
//            DataOutputStream out = new DataOutputStream(
//                    connection.getOutputStream());
//            JSONObject obj = new JSONObject();
//            obj.put("userid",USERID);
//            long time=System.currentTimeMillis();
//            obj.put("ts", time);
//            obj.put("sign", ToolMD5.encodeMD5Hex(USERID+time+APIKEY) );
//            obj.put("mobile",mobile);
//            obj.put("templateid",TEMPLATEID);
//            JSONArray arr = new JSONArray();
//            arr.add(code);
//            obj.put("templateparam",arr);
//            out.writeBytes(obj.toJSONString());
//            out.flush();
//            out.close();
//            //读取响应
//            BufferedReader reader = new BufferedReader(new InputStreamReader(
//                    connection.getInputStream()));
//            String lines;
//            StringBuffer sb = new StringBuffer("");
//            while ((lines = reader.readLine()) != null) {
//                lines = new String(lines.getBytes(), "utf-8");
//                sb.append(lines);
//            }
//            reader.close();
//            // 断开连接
//            connection.disconnect();
//            JSONObject obj1=JSON.parseObject(sb.toString());
//            if (obj1.getIntValue("code")==0){
//                return true;
//            }else{
//                return false;
//            }
//        } catch (MalformedURLException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//            return false;
//        } catch (UnsupportedEncodingException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//            return false;
//        } catch (IOException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//            return false;
//        }
//    }






}
